package com.atguigu.system.controller;


import com.atguigu.admin.model.entity.SysUser;
import com.atguigu.admin.service.SysUserService;
import com.atguigu.common.result.Result;
import com.atguigu.common.utils.JwtUtils;
import com.atguigu.system.model.entity.ProductInfoFlow;
import com.atguigu.system.model.entity.Sku;
import com.atguigu.system.service.ProductInfoFlowService;
import com.atguigu.system.service.SkuService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 
 * @since 2025-03-01
 */
@RestController
@RequestMapping("/system/sku")
public class SkuController {

    @Autowired
    private SkuService skuService;

    @Autowired
    private ProductInfoFlowService flowService;

    @Autowired
    private SysUserService sysUserService;

    @PostMapping("/save")
    public  Result<?> saveSku(@RequestBody Sku sku,HttpServletRequest request) {
        int insert = skuService.getBaseMapper().insert(sku);
        ProductInfoFlow productInfoFlow = getProductInfoFlow(request,"SKU","新增",sku.getId());
        flowService.save(productInfoFlow);
        return Result.ok();
    }


    @PutMapping("/update")
    public Result<?> updateSku(@RequestBody Sku sku, HttpServletRequest request) {
        ProductInfoFlow productInfoFlow = getProductInfoFlow(request,"SKU","更新",sku.getId());
        flowService.save(productInfoFlow);
        return Result.ok(skuService.updateSku(sku));
    }

    @DeleteMapping("/remove/{skuId}")
    public  Result<?> removeSkuById(@PathVariable Integer skuId, HttpServletRequest request) {
        ProductInfoFlow productInfoFlow = getProductInfoFlow(request,"SKU","删除",skuId);
        flowService.save(productInfoFlow);
        boolean b = skuService.removeSkuById(skuId);
        return Result.ok(b);
    }


    @GetMapping("/get/{skuId}")
    public  Result<?> getSkuById(@PathVariable Integer skuId) {
        return Result.ok(skuService.getSkuById(skuId));
    }

    @GetMapping("/listAll")
    public  Result<?> listAllSkus() {
        return Result.ok(skuService.listAllSkus());
    }

    @GetMapping("/page")
    public  Result<?> pageSkus(@RequestParam(defaultValue = "1") long current,
                               @RequestParam(defaultValue = "10") long size,
                               @RequestParam(required = false) String color,
                               @RequestParam(required = false) String spuId) {
        Page<Sku> page = new Page<>(current, size);
        return Result.ok(skuService.pageSkus(page,spuId,color));
    }

    private ProductInfoFlow getProductInfoFlow(HttpServletRequest request,String ObjectType ,String OperatorType,Integer id) {
        ProductInfoFlow productInfoFlow = new ProductInfoFlow();
        String userId = JwtUtils.getUserId(request.getHeader("token"));
        SysUser byId = sysUserService.getById(userId);
        productInfoFlow.setObjectType(ObjectType);
        productInfoFlow.setObjectId(id);
        productInfoFlow.setOperatorId(Math.toIntExact(byId.getId()));
        productInfoFlow.setOperatorName(byId.getName());
        productInfoFlow.setOperationType(OperatorType);
        return productInfoFlow;
    }

}

